package kr.or.kead.ui.insert_update;

import java.awt.Color;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.ArrayList;
import java.util.StringTokenizer;

import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

import kr.or.kead.domain.Auth;
import kr.or.kead.domain.Course;
import kr.or.kead.domain.Depart;
import kr.or.kead.domain.Professor;
import kr.or.kead.service.DaoCourse;
import kr.or.kead.service.DaoProfessor;
import kr.or.kead.service.Daodepart;
import kr.or.kead.ui.MainFrame;

public class CourseInsertUpdate extends AbsInsertUpdate {
	

	private JLabel subject_codel;
	private JLabel subject_code;
	private JLabel lblNewLabel_2;
	private JComboBox<Object> comboDepartCode;
	private JLabel lblNewLabel_3;
	private JTextField txtSubject;
	private JLabel lblNewLabel_4;
	private JTextField txtMaterial;
	private JLabel lblNewLabel_5;
	private JComboBox<Object> comboProfCode;
	private Daodepart daoDepart;
	private DaoProfessor daoProfessor;
	private Auth auth;
	private int level;
	public CourseInsertUpdate(Course course) {
		super(course, "강좌정보");
	}
	
	@Override
	protected void initDao() {
		daotable = new DaoCourse();
		daoDepart = new Daodepart();
		daoProfessor = new DaoProfessor();
		auth= MainFrame.getAuth();
		level= auth.getLevel();
	}

	@Override
	protected boolean isValidCheck() {
		if (comboDepartCode.getSelectedIndex()==0){
			JOptionPane.showMessageDialog(null, "학과를 선택하세요");
			return false;
		}
		if (txtSubject.getText().equals("")|| txtMaterial.getText().equals("")){
			JOptionPane.showMessageDialog(null, "빈칸이 있습니다.");
			txtSubject.requestFocus();
			return false;
		} 
		return true;
	}

	@Override
	protected JPanel getMainPanel() {
		JPanel centerPanel = new JPanel();
		centerPanel.setLayout(new GridLayout(5, 2, 5, 0));
		
		subject_codel = new JLabel("과목 코드 :");
		subject_codel.setForeground(Color.BLACK);
		subject_codel.setFont(new Font("Rix계란후라이 M", Font.BOLD, 20));
		subject_codel.setHorizontalAlignment(SwingConstants.CENTER);
		centerPanel.add(subject_codel);
		
		subject_code = new JLabel("");
		centerPanel.add(subject_code);
		
		lblNewLabel_2 = new JLabel("학       과 :");
		lblNewLabel_2.setFont(new Font("Rix계란후라이 M", Font.BOLD, 20));
		lblNewLabel_2.setHorizontalAlignment(SwingConstants.CENTER);
		centerPanel.add(lblNewLabel_2);
		
		ArrayList<String> departs = daoDepart.selectTableAllNames();
		departs.add(0, "학과 선택");
		Object[] data = departs.toArray();
		comboDepartCode = new JComboBox<Object>(data);
		comboDepartCode.addItemListener(new ItemListener() {
			public void itemStateChanged(ItemEvent e) {
				String res = (String) comboDepartCode.getSelectedItem(); //학과 전자
				if (res.equals("학과 선택"))return;
				int code = ((DaoCourse)daotable).selectMaxCode(daoDepart.selectCodeDepartByName(res));
				if (code==0) {
					code = daoDepart.selectCodeDepartByName(res)*1000;
				}
				subject_code.setText(String.valueOf(code+1));
			}
		});
		centerPanel.add(comboDepartCode);
		
		lblNewLabel_3 = new JLabel("과  목  명 :");
		lblNewLabel_3.setFont(new Font("Rix계란후라이 M", Font.BOLD, 20));
		lblNewLabel_3.setHorizontalAlignment(SwingConstants.CENTER);
		centerPanel.add(lblNewLabel_3);
		
		txtSubject = new JTextField();
		centerPanel.add(txtSubject);
		txtSubject.setColumns(10);
		
		lblNewLabel_4 = new JLabel("교  재  명 :");
		lblNewLabel_4.setFont(new Font("Rix계란후라이 M", Font.BOLD, 20));
		lblNewLabel_4.setHorizontalAlignment(SwingConstants.CENTER);
		centerPanel.add(lblNewLabel_4);
		
		txtMaterial = new JTextField();
		centerPanel.add(txtMaterial);
		txtMaterial.setColumns(10);
		
		lblNewLabel_5 = new JLabel("교  수  명  :");
		lblNewLabel_5.setFont(new Font("Rix계란후라이 M", Font.BOLD, 20));
		lblNewLabel_5.setHorizontalAlignment(SwingConstants.CENTER);
		centerPanel.add(lblNewLabel_5);
		ArrayList<String> arTeachers = new ArrayList<>();
		if(level == 2){
			Professor prof = (Professor)daoProfessor.selectTableByEmail(auth.getEmail());
			arTeachers.add(prof.getCode() + ":" + prof.getName());
		}else {
			arTeachers = daoProfessor.selectTableAllIdx();
		}		
		Object[] teachers = arTeachers.toArray();
		comboProfCode = new JComboBox<Object>(teachers);
		centerPanel.add(comboProfCode);
		
		return centerPanel;
	}

	@Override
	protected Object getObject() {
		if (obj==null){
			StringTokenizer st = new StringTokenizer((String)comboProfCode.getSelectedItem(),":");
			int teacherCode = Integer.parseInt(st.nextToken().trim());
			Course course = new Course(
					Integer.parseInt(subject_code.getText()), 
					daoDepart.selectCodeDepartByName((String) comboDepartCode.getSelectedItem()),
					txtSubject.getText().trim(), 
					txtMaterial.getText().trim(),
					teacherCode
					);
			return course;
		}else{
			Course course=(Course)obj;
			course.setCode(Integer.parseInt(subject_code.getText()));
			course.setDepart_code(daoDepart.selectCodeDepartByName((String) comboDepartCode.getSelectedItem()));
			course.setSubject(txtSubject.getText().trim());
			course.setMaterial(txtMaterial.getText().trim());
			StringTokenizer st = new StringTokenizer((String)comboProfCode.getSelectedItem(), ":");
			Professor professor = (Professor) daoProfessor.selectTableById(Integer.parseInt(st.nextToken().trim()));
			course.setProf_code(professor.getCode());
			return course;
		}
	}

	@Override
	protected void updateInit() {
		Course course = (Course) obj;
		Depart depart = (Depart) daoDepart.selectTableById(course.getDepart_code());
		comboDepartCode.setSelectedItem(depart.getName());
		subject_code.setText(String.valueOf(course.getCode()));
		txtSubject.setText(course.getSubject());
		txtMaterial.setText(course.getMaterial());
		comboProfCode.setSelectedItem(daoProfessor.selectTableIdxNameByCode(course.getProf_code()));
	}

}
